{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chat Summary Memory Buffer\n",
    "\n",
    "**NOTE:** This example of memory is deprecated in favor of the newer and more flexible `Memory` class. See the [latest docs](https://docs.llamaindex.ai/en/stable/module_guides/deploying/agents/memory/).\n",
    "\n",
    "The `ChatSummaryMemoryBuffer` is a memory buffer that stores the last X messages that fit into a token limit. It also summarizes the chat history into a single message.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-core"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.memory import ChatSummaryMemoryBuffer\n",
    "\n",
    "memory = ChatSummaryMemoryBuffer.from_defaults(\n",
    "    token_limit=40000,\n",
    "    # optional set the summary prompt, here's the default:\n",
    "    # summarize_prompt=(\n",
    "    #     \"The following is a conversation between the user and assistant. \"\n",
    "    #     \"Write a concise summary about the contents of this conversation.\"\n",
    "    # )\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using Standalone"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.llms import ChatMessage\n",
    "\n",
    "chat_history = [\n",
    "    ChatMessage(role=\"user\", content=\"Hello, how are you?\"),\n",
    "    ChatMessage(role=\"assistant\", content=\"I'm doing well, thank you!\"),\n",
    "]\n",
    "\n",
    "# put a list of messages\n",
    "memory.put_messages(chat_history)\n",
    "\n",
    "# put one message at a time\n",
    "# memory.put_message(chat_history[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get the last X messages that fit into a token limit\n",
    "history = memory.get()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get all messages\n",
    "all_history = memory.get_all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# clear the memory\n",
    "memory.reset()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using with Agents\n",
    "\n",
    "You can set the memory in any agent in the `.run()` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"sk-proj-...\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.agent.workflow import ReActAgent, FunctionAgent\n",
    "from llama_index.core.workflow import Context\n",
    "from llama_index.llms.openai import OpenAI\n",
    "\n",
    "\n",
    "memory = ChatMemoryBuffer.from_defaults(token_limit=40000)\n",
    "\n",
    "agent = FunctionAgent(tools=[], llm=OpenAI(model=\"gpt-4o-mini\"))\n",
    "\n",
    "# context to hold the chat history/state\n",
    "ctx = Context(agent)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "resp = await agent.run(\"Hello, how are you?\", ctx=ctx, memory=memory)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ChatMessage(role=<MessageRole.USER: 'user'>, additional_kwargs={}, blocks=[TextBlock(block_type='text', text='Hello, how are you?')]), ChatMessage(role=<MessageRole.ASSISTANT: 'assistant'>, additional_kwargs={}, blocks=[TextBlock(block_type='text', text=\"Hello! I'm just a program, so I don't have feelings, but I'm here and ready to help you. How can I assist you today?\")])]\n"
     ]
    }
   ],
   "source": [
    "print(memory.get_all())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llama-index-caVs7DDe-py3.10",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
